import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MyArraylist {

    public int[] elem;
    public int usedSize;

    public MyArraylist() {
        this.elem = new int[5];
    }

    /**
     * 打印顺序表:
     * 根据usedSize判断即可
     */
    public void display() {
        for (int i = 0; i < this.usedSize; i++) {
            System.out.print(elem[i] + " ");
        }
        System.out.println();
    }

    // 新增元素,默认在数组最后新增
    public void add(int data) {
        if(isFull()) {
            resize();
        }
        this.elem[usedSize] = data;
        this.usedSize++;
    }

    public void resize() {
        this.elem = Arrays.copyOf(elem,elem.length*2);
    }

    /**
     * 判断当前的顺序表是不是满的！
     *
     * @return true:满   false代表空
     */
    public boolean isFull() {
        return this.usedSize == this.elem.length;
    }

    private boolean checkPosInAdd(int pos) {

        if(pos < 0 || pos > this.usedSize) {
            System.out.println("输入的位置不合法!");
            return false;
        }
        return true;
    }

    // 在 pos 位置新增元素
    public void add(int pos, int data) {
        if(isFull()) {
            resize();
        }
        if(!checkPosInAdd(pos)) {
            throw new MyArrayListIndexOfException("pos位置不合法, pos: " + pos);
        }
        for (int i = this.usedSize; i > pos; i--) {
            this.elem[i] = this.elem[i-1];
        }
        this.elem[pos] = data;
        this.usedSize++;
    }

    // 判定是否包含某个元素
    public boolean contains(int toFind) {
        for (int i = 0; i < this.usedSize; i++) {
            if(this.elem[i] == toFind) {
                return true;
            }
        }
        return false;
    }

    // 查找某个元素对应的位置
    public int indexOf(int toFind) {
        for (int i = 0; i < this.usedSize; i++) {
            if(this.elem[i] == toFind) {
                return i;
            }
        }
        return -1;
    }

    // 获取 pos 位置的元素
    public int get(int pos) {
        if(!checkPosInGet(pos)) {
            throw new MyArrayListIndexOfException("pos位置不合法,此时pos: " + pos);
        }
        if(isEmpty()) {
            throw new MyArrayListEmptyException("获取元素时, 顺序表为空! ");
        }
        return this.elem[pos];
    }

    public boolean checkPosInGet(int pos) {
        if(pos < 0 || pos >= this.usedSize) {
            return false;
        }
        return true;
    }
    private boolean isEmpty() {
       return this.usedSize == 0;
    }

    // 给 pos 位置的元素设为【更新为】 value
    public void set(int pos, int value) {
        if(!checkPosInGet(pos)) {
            throw new MyArrayListIndexOfException("pos位置不合法,此时pos: "+ pos);
        }
        if(isEmpty()) {
            throw new MyArrayListEmptyException("获取元素时, 顺序表为空! ");
        }
        this.elem[pos] = value;
    }

    /**
     * 删除第一次出现的关键字key
     *
     * @param key
     */
    public void remove(int key) {
        if(isEmpty()) {
            throw new MyArrayListEmptyException("顺序表为空,不能删除");
        }
        int index = indexOf(key);
        if(index == -1) {
            System.out.println("没有你要删除的元素! ");
            return;
        }
        for (int i = index; i < this.usedSize-1; i++) {
            this.elem[i] = this.elem[i+1];
        }
        this.usedSize--;
    }

    // 获取顺序表长度
    public int size() {
        return this.usedSize;
    }

    // 清空顺序表
    public void clear() {
        this.usedSize = 0;
    }

    public static void main(String[] args) {
        MyArraylist myArraylist = new MyArraylist();
        myArraylist.add(1);
        myArraylist.add(2);
        myArraylist.add(3);
        myArraylist.add(4);
        myArraylist.add(5);
        myArraylist.add(6);
        /*try{
            myArraylist.add(-1,10);
        }catch (MyArrayListIndexOfException e) {
            e.printStackTrace();
        }*/
        System.out.println(myArraylist.contains(2));
        myArraylist.display();
        // List<String> list = new ArrayList<>();
        int a = 3;
        if(a < 0 || a > 4) {
            System.out.println(a);
        }
    }
}